import requests
from lxml import etree

for num in range(1,5):
    url = 'https://www.qiushibaike.com/text/page/' + str(num)

    req = requests.get(url)
    data = req.text
    html = etree.HTML(data)
    xpath_div = html.xpath('//div[@class="article block untagged mb15 typs_long"]')
    #print(xpath_div)
    for div in xpath_div:
        author = div.xpath('//a/h2/text()')  #作者
        print(author)
        
        content = div.xpath('//div[@class="content"]/span/text()')#内容
        print(content)
        funny_count = div.xpath('//span[@class="stats-vote"]/i/text()') #好笑数
        print(funny_count)
        comment_count = div.xpath('//span[@class="stats-comments"]/a/i/text()')#评论数
        print(div.xpath('//span[@class="stats-comments"]/a/i/text()'))
        
        with open('爬虫\\糗事百科.txt','a',encoding = 'utf8') as f:
            for i in range(len(author)):
                f.write('作者：')
                f.write( author[i].strip())
                f.write('\n')
                f.write('内容：')
                f.write(content[i].strip())
                f.write('\n')
                f.write('好笑数：')
                f.write( funny_count[i])
                f.write('   ')
                f.write('评论数：')
                f.write( comment_count[i])
                f.write('\n')
                f.write('==============')
                f.write('\n')



